using System;
using AsyncLibrary.StatusListener;

namespace AsyncLibrary
{
  /// <summary>Run <see cref="IOperation"/> synchronously</summary>
  public class SyncRuner
  {
    /// <summary>Run <see cref="IOperation"/> synchronously</summary>
    public static void Run(IOperation operation)
    {
      IAsyncOperation asyncOperation = operation as IAsyncOperation;
      if (asyncOperation != null)
        if (asyncOperation.StatusListeners.Count == 0)
          asyncOperation.StatusListeners.Add(new LocalListener());
      operation.Run(new OperationContext());
    }

    private class LocalListener : StatusListenerBase
    {
      public override void FatalError(Exception ex)
      {
        throw ex;
      }
    }
  }
}